<?php

/*
 * Copyright (C) 2010 Emanuele Colonnelli <emanuele.colonnelli@phpstruct.org>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace org\phpstruct\core\dto;

use org\phpstruct\core\Error;

/**
 * Description of ErrorData
 *
 * @author Emanuele Colonnelli <emanuele.colonnelli@phpstruct.org>
 */
class ErrorData {

    private $code;
    private $message;
    private $exception;
    private $type;
    private $format = 'JSON';

    function __construct($code, $message, \Exception $exception = null, $type=null) {
        $this->code = $code;
        $this->message = $message;
        $this->exception = $exception;
        $this->type = $type;
    }

    public function setFormat($format) {
        $this->format = $format;
    }

    public function getFormat() {
        return $this->format;
    }

    public function getCode() {
        return $this->code;
    }

    public function setCode($code) {
        $this->code = $code;
    }

    public function getMessage() {
        $out = null;
        if (is_array($this->message)) {
            foreach ($this->message as $msg) {
                $out .= "[ " . implode(',', $this->message) . " ]\r\n";
            }
        } else {
            $out .= "[{$this->message}]\r\n";
        }
        return $out;
    }

    public function setMessage($message) {
        $this->message = $message;
    }

    public function getException() {
        return $this->exception;
    }

    public function setException($exception) {
        $this->exception = $exception;
    }

    public function getType() {
        return $this->type;
    }

    public function setType($type) {
        $this->type = $type;
    }

    public function __toString() {

        $out = "";

        $type = 'ERROR';
        switch ($this->type) {
            case Error::ERROR_TYPE_ERROR:
                $type = 'ERROR';
                break;
            case Error::ERROR_TYPE_WARNING:
                $type = 'WARNING';
                break;
            case Error::ERROR_TYPE_NOTICE:
                $type = 'NOTICE';
                break;
            case Error::ERROR_TYPE_DEPRECATED:
                $type = 'DEPRECATED';
                break;
        }
        $out.= "[ERROR][TYPE] {$type}\r\n";
        $out.= "[ERROR][CODE] {$this->code}\r\n";

        $out.= $this->getMessage();
        
        if ($this->exception instanceof \Exception) {
            $out.= "[ERROR][STACKTRACE]{$this->exception->getTraceAsString()}\r\n";
        }

        return $out;
    }

}
